package com.jsoup.controller.admin;

import com.jsoup.bean.User;
import com.jsoup.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/adminManage")
public class AdminManageController {

    @Resource
    private UserService userService;

    /**
     * 检查旧密码是否输入正确
     * 加密输入的，然后查询数据库是否存在
     *
     * @param user
     * @return
     */
    @RequestMapping("/checkAdminPassword")
    @RequiresRoles("admin")
    @ResponseBody
    public Object checkAdminPassword(User user) {
        Map<Object, Object> map = new HashMap<>();
        Md5Hash md5UserPassword = new Md5Hash(user.getUserPassword(), ByteSource.Util.bytes(user.getUserName()), 1024);
        user.setUserPassword(md5UserPassword.toString());
        User u = userService.getUser(user);
        if (u != null) {
            map.put("result", "success");
        }
        return map;

    }

    /**
     * 更新密码
     *
     * @param user
     * @return
     */
    @RequestMapping("/updateAdminPassword")
    @RequiresRoles("admin")
    @ResponseBody
    public Object updateAdminPassword(User user) {
        Map<Object, Object> map = new HashMap<>();
        Md5Hash md5UserPassword = new Md5Hash(user.getUserPassword(), ByteSource.Util.bytes(user.getUserName()), 1024);
        user.setUserPassword(md5UserPassword.toString());
        int count = userService.updateByPrimaryKeySelective(user);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 更新基本信息
     *
     * @param user
     * @return
     */
    @RequestMapping("/updateAdmin")
    @RequiresRoles("admin")
    @ResponseBody
    public Object updateAdmin(User user) {
        Map<Object, Object> map = new HashMap<>();
        Md5Hash md5UserPassword = new Md5Hash(user.getUserPassword(), ByteSource.Util.bytes(user.getUserName()), 1024);
        user.setUserPassword(md5UserPassword.toString());
        int count = userService.updateByPrimaryKeySelective(user);
        if (count > 0) {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            User u = new User();
            u.setUserId(user.getUserId());
            User currentUser = userService.getUser(u);
            session.setAttribute("currentUser", currentUser);
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 判断用户名、邮箱是否已注册，与用户注册是的接口不同，这里用于用户修改资料，需要排除当前用户的记录
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("/checkAdminRepeat")
    @RequiresRoles("admin")
    @ResponseBody
    public Object checkUserRepeat(User userInfo) {
        Map<Object, Object> map = new HashMap<>();
        int i = 0;
        int j = 0;
        User user = new User();
        user.setUserName(userInfo.getUserName());
        User count = userService.getUser(user);
        if (count != null && count.getUserId() != userInfo.getUserId()) {
            i += 1;
        }
        User user1 = new User();
        user1.setUserEmail(userInfo.getUserEmail());
        User count1 = userService.getUser(user1);
        if (count1 != null && count1.getUserId() != userInfo.getUserId()) {
            j += 1;
        }
        if (i > 0 && j == 0) {
            map.put("result", "该用户名已经被注册");
        }
        if (i == 0 && j > 0) {
            map.put("result", "该邮箱已经被注册");
        }
        if (i > 0 && j > 0) {
            map.put("result", "该用户名、邮箱都已经被注册");
        }
        return map;
    }

    /**
     * 个人中心页面
     *
     * @return
     */
    @RequestMapping("/myself")
    @RequiresRoles("admin")
    public String myself() {
        return "manage-myself";
    }

    /**
     * 控制台页面
     *
     * @return
     */
    @RequestMapping("/index")
    @RequiresRoles("admin")
    public String manage() {
        return "manage-console";
    }

}
